package com.gitee.zeric.tenantdb.demo.service.impl;

import com.gitee.zeric.tenantdb.demo.entity.TenantInfo;
import com.gitee.zeric.tenantdb.demo.service.ITenantInfoService;
import com.gitee.zeric.tenantdb.handler.ITenantInfoHandler;
import com.gitee.zeric.tenantdb.handler.TenantDataSourceProperties;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 获取初始化租户信息
 *
 * @author zj
 * @date 2022/11/25
 */
@Component
public class TenantInfoHandlerImpl implements ITenantInfoHandler {

    @Autowired
    ITenantInfoService iTenantInfoService;

    @Override
    public List<TenantDataSourceProperties> list() {
        List<TenantInfo> list = iTenantInfoService.list();

        return list.stream().map(t-> new TenantDataSourceProperties(t.getId().toString(),t.getDatasourceUrl(),t.getDatasourceUsername(),t.getDatasourcePassword(),t.getDatasourceDriver(),t.getDatasourceClass())).collect(Collectors.toList());
    }

    @Override
    public String dynamicTenantId() {
        return "1";
    }

}
